<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Group extends MX_Controller{
        
        protected $_templates;
        protected $_module_name = "";
        
        function __construct() {
            parent::__construct();

            $this->_module_name = $this->router->fetch_module();
            $this->pre_message ="";
            $this->lang->load('access');
            $this->load->model('group_model','group');
            $this->session->set_userdata(array('Url'=>uri_string()));          
        }
        function index($page=0){
            $this->acl->check('view','','',base_url());
            if($this->acl->check('add'))
                $data['add'] = $this->_module_name.'/group/add';
            
            $data['title'] = lang('group.list');
            $data['page']  = (int)$page;
            
            $data['_module_name']=$this->_module_name;
            
            $data['list'] = $this->group->get_all_group();
            $data['num'] = count($data['list']);

            $this->_templates['page'] = 'group/list';
            $this->site_lib->load($this->_templates['page'],$data);
        }
        
        function data_group(){
          $data=array();
          $data['group_name']    = $this->input->post('group_name');
          $data['group_comment'] = $this->input->post('group_comment');

          return $data;
        }
        function data_right($post_var){
            $data=array();
            $data['group_id']=$post_var['group_id'];
            $data['module_id']=$post_var['module_id'];
            $data['function_id']=$post_var['function_id'];
            return $data;
        }
        /*
         * @author : Vo Van Hieu
         * @method : add new group
         * @date   : 19/10/2012
         * @modify : 
         */
        function add(){
            $this->acl->check('add','','',base_url());
            
            $data['title'] = lang('group.add_new_group');

            if(!empty($_POST['bt_submit'])){ 
               $this->form_validation->set_rules('group_name',lang('group.name'),'trim|required'); 
               if($this->form_validation->run() == FALSE){
                  $this->pre_message = validation_errors();
               }else{
                    $data_group=$this->data_group();
                    $group_id= $this->group->save(0,$data_group);
                    if($group_id){
                        $arr_right = $this->input->post('arr_right');
                        $arr_permission = $this->input->post('arr_permission');
                        for($i = 0; $i < sizeof($arr_right); $i ++) {
                            $arr_right[$i]['group_id']=$group_id;
                            $data_right = $this->data_right($arr_right[$i]);
                            $data_right['permission']=json_encode($arr_permission[$i]);
                            $this->group->save_right($arr_right[$i]['right_id'],$data_right);
                        }
                       $this->session->set_flashdata('message',lang('admin.save_successful'));
                       redirect($this->_module_name.'/group');
                    }else{
                       $this->pre_message = lang('admin.save_unsuccessful');
                    }
               } 
            }
            $data['message'] = $this->pre_message;
            $data['modules'] = $this->group->get_all_module();
            
            $data['permission']=$this->group->getAllPermission();
            $arr_code_permission = array();
            if(count($data['permission'])>0){
                foreach($data['permission'] as $pm_code){
                    $arr_code_permission[]=$pm_code->permission_code;
                }
            }
            $data['arr_pm_code']=$arr_code_permission;
            
            $this->_templates['page'] = 'group/add';
            $this->site_lib->load($this->_templates['page'],$data);
        }
          /*
         * @author : Vo Van Hieu
         * @method : edit group
         * @date   : 19/10/2012
         * @modify : 
         */
        function edit($id){
          $this->acl->check('edit','','',base_url());  
            
          $data['title'] = lang('group.update_group');
          $data['rs'] = $this->group->get_group_by_id($id);
          if(!empty($_POST['bt_submit'])){
             $this->form_validation->set_rules('group_name',lang('group.name'),'trim|required');
             
             if($this->form_validation->run() == FALSE){
                $this->pre_message = validation_errors();
             }else{  
                $data_group=$this->data_group();
                if($this->group->save($id,$data_group)){
                    $arr_right = $this->input->post('arr_right');
                    $arr_permission = $this->input->post('arr_permission');
					if((!empty($arr_permission)) && (!empty($arr_right))){
						for($i = 0; $i < sizeof($arr_right); $i ++) {
							$data_right = $this->data_right($arr_right[$i]);
							if(isset($arr_permission[$i])&&!$arr_permission[$i]){
								$arr_permission[$i]=array();
								
							}
							if(isset($arr_permission[$i])){
								$data_right['permission']=json_encode($arr_permission[$i]);
								$this->group->save_right($arr_right[$i]['right_id'],$data_right);
							}
						}
					}
                    $this->session->set_flashdata('message',lang('admin.save_successful'));
                    redirect($this->_module_name.'/group');
                }else{
                    $this->pre_message = lang('admin.save_unsuccessful');
                }
             } 
          }
          $data['message'] = $this->pre_message;     
          $data['modules'] = $this->group->get_all_module();
          
          $data['permission']=$this->group->getAllPermission();
            $arr_code_permission = array();
            if(count($data['permission'])>0){
                foreach($data['permission'] as $pm_code){
                    $arr_code_permission[]=$pm_code->permission_code;
                }
            }
            $data['arr_pm_code']=$arr_code_permission;
          
          $this->_templates['page'] = 'group/edit';
          $this->site_lib->load($this->_templates['page'],$data);
        }  
        /*
         * @author : Vo Van Hieu
         * @method : delete group
         * @date   : 19/10/2012
         * @modify : 
         */
        function del($id){
            $this->acl->check('del','','',base_url());
            //$page = (int)$this->input->post('page');

            if($this->group->delete($id))
                $this->session->set_flashdata('message',lang('admin.delete_successful'));
            else 
                $this->session->set_flashdata('message',lang('admin.delete_unsuccessful'));

            redirect($this->_module_name.'/group');
        }
        /*
         * @author : Vo Van Hieu
         * @method : delete groups
         * @date   : 19/10/2012
         * @modify : 
         */
        function dels(){
            $this->acl->check('dels','','',base_url());
            
            if(!empty($_POST['ar_id']))
            {
                //$page = (int)$this->input->post('page');
                $ar_id = $this->input->post('ar_id');

                if(!empty($_POST['btn_submit']))
                {
                    for($i = 0; $i < sizeof($ar_id); $i ++) {
                        if ($ar_id[$i]){
                            if($this->group->delete($ar_id[$i]))
                                $this->session->set_flashdata('message',lang('admin.delete_successful'));
                            else 
                                $this->session->set_flashdata('message',lang('admin.delete_unsuccessful'));
                        }
                    }
                }
            }
            redirect($this->_module_name.'/group');
        }
    }
/* End of file group.php */
/* Location: ./application/webadmin/groups/system/controllers/group.php */